package cn.lhl.service.company.impl;

import cn.lhl.common.entity.PageBean;
import cn.lhl.dao.company.CompanyDao;
import cn.lhl.domain.company.Company;
import cn.lhl.service.company.CompanyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    private CompanyDao companyDao;

    public List<Company> findAll() {
        List<Company> companyList = companyDao.findAll();
        return companyList;
    }

    public void save(Company company) {
        companyDao.save(company);
    }

    public Company findById(String id) {
        Company company = companyDao.findById(id);
        return company;
    }

    public void update(Company company) {
        companyDao.update(company);
    }

    public void delete(String id) {
        companyDao.delete(id);
    }

    //page当前页码  size页面条数
    public PageBean findPageBean(int page, int size) {
        //PageBean中有两个参数需要调用数据库进行查询,list和total
        long total =  companyDao.findCount();//查询有多少条记录total
        //计算索引
        int index = (page-1)*size;
        //得出数据
        List<Company> companyList = companyDao.findByPage(index,size);
        PageBean pageBean = new PageBean(total, companyList, page, size);
        return pageBean;
    }

    //使用PageHelper
    public PageInfo findByPageHelper(int page,int size){
        //调用PageHelper.startPage方法
        PageHelper.startPage(page,size);
        //紧跟着的第一个查询会被分页
        List<Company> list = companyDao.findAll();
        //返回
        return new PageInfo(list);
    }
}
